﻿@page "/"
@inject IMediator Mediator
@inject NavigationManager NavManager

<PageTitle>Blazing Trails</PageTitle>

@if (_trails == null)
{
    <p>Loading trails...</p>
}
else
{
    <TrailDetails Trail="_selectedTrail" />

    <TrailSearch />

    @if (_trails.Any())
    {
        <AuthorizeView>
            <Authorized>
                <div class="mb-4">
                    <p class="font-italic text-center">Do you have an awesome trail you'd like to share? <a href="add-trail">Add it here</a>.</p>
                </div>
            </Authorized>
            <NotAuthorized>
                <div class="mb-4">
                    <p class="font-italic text-center">Do you have an awesome trail you'd like to share? Please <a href="authentication/login">log in or sign up</a>.</p>
                </div>
            </NotAuthorized>
        </AuthorizeView>

        <ViewSwitcher Items="_trails" Context="trail">
            <GridTemplate>
                <TrailCard Trail="trail" OnSelected="HandleTrailSelected" />
            </GridTemplate>
            <HeaderTemplate>
                <th>Name</th>
                <th>Location</th>
                <th>Length</th>
                <th>Time</th>
                <th></th>
            </HeaderTemplate>
            <RowTemplate>
                <th scope="col">@trail.Name</th>
                <td>@trail.Location</td>
                <td>@(trail.Length)km</td>
                <td>@trail.TimeFormatted</td>
                <td class="text-right">
                    <AuthorizeView>
                        @if (trail.Owner.Equals(context.User.Identity?.Name, StringComparison.OrdinalIgnoreCase) || context.User.IsInRole("Administrator"))
                        {
                            <button @onclick="@(() => NavManager.NavigateTo($"/edit-trail/{trail.Id}"))" title="Edit" class="btn btn-outline-secondary">
                                <i class="bi bi-pencil"></i>
                            </button>
                        }
                    </AuthorizeView>
                    <FavoriteButton Trail="trail" />
                    <button @onclick="@(() => HandleTrailSelected(trail))" title="View" class="btn btn-primary">
                        <i class="bi bi-binoculars"></i>
                    </button>
                </td>
            </RowTemplate>
        </ViewSwitcher>
    }
    else
    {
        <NoTrails>
            We currently don't have any trails, <a href="add-trail">why not add one?</a>
        </NoTrails>
    }
}

@code {
    private IEnumerable<Trail>? _trails;
    private Trail? _selectedTrail;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            var response = await Mediator.Send(new GetTrailsRequest());
            _trails = response.Trails.Select(x => new Trail
            {
                Id = x.Id,
                Name = x.Name,
                Image = x.Image,
                Description = x.Description,
                Location = x.Location,
                Length = x.Length,
                TimeInMinutes = x.TimeInMinutes,
                Owner = x.Owner,
                Waypoints = x.Waypoints.Select(wp => new BlazingTrails.ComponentLibrary.Map.LatLong(wp.Latitude, wp.Longitude))?.ToList() ?? new List<ComponentLibrary.Map.LatLong>()
            });
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"There was a problem loading trail data: {ex.Message}");
        }
    }

    private void HandleTrailSelected(Trail trail)
        => _selectedTrail = trail;
}
